package com.daffodil.flowable.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.core.annotation.AuthPermission;
import com.daffodil.core.annotation.Log;
import com.daffodil.core.annotation.Log.BusinessType;
import com.daffodil.core.controller.BaseController;
import com.daffodil.core.entity.JsonResult;
import com.daffodil.core.entity.Page;
import com.daffodil.core.entity.TableInfo;
import com.daffodil.flowable.constant.FlowableConstant;
import com.daffodil.flowable.entity.ActCnDeployment;
import com.daffodil.flowable.entity.ActCnModel;
import com.daffodil.flowable.service.IFlowableModelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 流程模板管理控制器
 * @author yweijian
 * @date 2021年11月16日
 * @version 1.0
 * @description
 */
@Api(value = "流程模板管理", tags = "流程模板管理")
@RestController
@RequestMapping(FlowableConstant.API_CONTENT_PATH)
public class FlowableModelerController extends BaseController {
    
    @Autowired
    private IFlowableModelService modelService;
    
    @ApiOperation("查询待发布流程列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("flowable:modeler:list")
    @GetMapping("/modeler/list/undeploy")
    public TableInfo list(ActCnModel actCnModel, @ApiIgnore HttpServletRequest request){
        initQuery(actCnModel);
        List<ActCnModel> list = modelService.selectModelUndeployList(actCnModel);
        return initTableInfo(list, query);
    }
    
    @ApiOperation("分页查询已发布流程列表")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @SuppressWarnings("unchecked")
    @AuthPermission("flowable:modeler:list")
    @GetMapping("/modeler/list/deploy")
    public TableInfo list(ActCnDeployment deployment, Page page, @ApiIgnore HttpServletRequest request){
        initQuery(deployment, page);
        List<ActCnDeployment> list = modelService.selectModelDeployList(query);
        return initTableInfo(list, query);
    }
    
    @ApiOperation("发布流程")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("flowable:modeler:deploy")
    @Log(title = "流程模板管理", businessType = BusinessType.INSERT)
    @PostMapping("/modeler/deploy/{modelId}/{category}")
    public JsonResult deploy(
            @ApiParam(value = "流程模板ID", required = true) @PathVariable("modelId") String modelId, 
            @ApiParam(value = "流程类型", required = true) @PathVariable("category") String category,
            @ApiIgnore HttpServletRequest request) {
        
        modelService.deployModeler(modelId, category);
        return JsonResult.success();
    }
    
    @ApiOperation("撤销发布流程")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @AuthPermission("flowable:modeler:undeploy")
    @Log(title = "流程模板管理", businessType = BusinessType.DELETE)
    @PostMapping("/modeler/undeploy/{deployId}")
    public JsonResult undeploy(@ApiParam(value = "流程发布ID", required = true) @PathVariable("deployId") String deployId, @ApiIgnore HttpServletRequest request) {
        modelService.removeDeployment(deployId);
        return JsonResult.success();
    }
    
    @ApiOperation("预览流程图")
    @ApiImplicitParam(name = HttpHeaders.AUTHORIZATION, value = "登录授权令牌 Bearer token", paramType = "header", required = true)
    @GetMapping("/modeler/diagram/{modelKey}")
    public void diagram(@ApiParam(value = "流程模板标识", required = true) @PathVariable("modelKey") String modelKey, @ApiIgnore HttpServletResponse response){
        try {
            InputStream in = modelService.getModelerDiagramByModelKey(modelKey);
            response.setHeader("Content-type", MediaType.IMAGE_PNG_VALUE);
            IOUtils.write(IOUtils.toByteArray(in), response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
